package com.panda.pandawash.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.panda.pandawash.R;
import com.panda.pandawash.application.CDApplication;
import com.panda.pandawash.bean.HttpResultBean;
import com.panda.pandawash.bean.StrongBoxBean;
import com.panda.pandawash.util.HttpUtil;
import com.panda.pandawash.view.StatusBarCompat;

import org.xutils.x;

/**
 * Created by gaowf on 2017/3/26.
 */

public class EnterSafeActivity extends Activity {

    private static String TAG = "EnterSafeActivity";

    private Button cancelButton;
    private Button confirmButton;
    private EditText inputPassword;
    private TextView forgetSafePsd;
    private String getPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setFinishOnTouchOutside(false);
        setContentView(R.layout.dialog_enter_safe);
        x.view().inject(this);
        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT) {
            StatusBarCompat.compat(this, getResources().getColor(R.color.colorPrimary));
        }

        inputPassword = (EditText) findViewById(R.id.input_safe_box_psd);
        forgetSafePsd = (TextView) findViewById(R.id.forget_safe_box_psd);
        forgetSafePsd.setOnClickListener(mOnClickListener);
        cancelButton = (Button) findViewById(R.id.cancel_psd_cancel);
        cancelButton.setOnClickListener(mOnClickListener);
        confirmButton = (Button) findViewById(R.id.enter_psd_confirm);
        confirmButton.setOnClickListener(mOnClickListener);
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.cancel_psd_cancel:
                    finish();
                    break;
                case R.id.forget_safe_box_psd:
                    Intent intent = new Intent(EnterSafeActivity.this, SafeForgetPsdActivity.class);
                    startActivity(intent);
                    finish();
                    break;
                case R.id.enter_psd_confirm:
                    getPassword = inputPassword.getText().toString();
                    loginStrongBoxPsw(getPassword);
//                    Intent enterIntent = new Intent(EnterSafeActivity.this, SafeBoxActivity.class);
//                    startActivity(enterIntent);
                    finish();
                    break;
            }
        }
    };
    private void loginStrongBoxPsw(String password){
        String account = CDApplication.getIntance().getUserInfo().getUseraccount();
        StrongBoxBean strongBoxBean = new StrongBoxBean();
        strongBoxBean.setSecretpassword(password);
        HttpUtil.loginStrongBoxPSW(new LoginSafeBoxCallback(),account,account,strongBoxBean);
    }

    private class LoginSafeBoxCallback extends AsyncHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, String content) {
            super.onSuccess(statusCode, content);
            Log.d(TAG, "LoginSafeBoxCallback statusCode = " + statusCode);
            Log.d(TAG, "LoginSafeBoxCallback onSuccess = " + content);
            if (statusCode == 200) {
                HttpResultBean resultBean = HttpUtil.parserHttpResultInfo(content, null);
                Log.d(TAG, "resultBean = " + resultBean);
                if (resultBean != null) {
                    String code = resultBean.getCode();
                    String message = resultBean.getMessage();
                    Log.d(TAG, "code = " + code);
                    if(code.equals("0")){
                        showMessage(getResources().getString(R.string.safe_box_login_success));
                        Intent intent = new Intent(EnterSafeActivity.this, SafeBoxActivity.class);
                        startActivity(intent);
                        finish();

                    }else if(code.equals("99")){
                        showMessage(message);
                    }
                }
            } else {
                showMessage(getResources().getString(R.string.safe_box_login_fail));
            }
        }

        @Override
        public void onFailure(Throwable error, String content) {
            super.onFailure(error, content);
            Log.d(TAG, "onFailure = " + error);
            if (error.toString().contains("refused")) {

            }
            // dialog.dismiss();
        }
    }

    private void showMessage(String string) {
        Toast toast = Toast.makeText(EnterSafeActivity.this, string, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }
}
